package toDo.app;

import interfaces.ICreateToDoActivity;

import java.util.List;

import toDo.app.models.MapLocation;
import toDo.app.models.TimeAuthority;
import toDo.app.models.ToDoListItem;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ViewToDoDialog  extends MapActivity implements OnClickListener{
	private MapView view;
	private TextView name, category,comment, discription, dueDate;
	private MapController mController;
	private Button editButton, okButton;
	private ToDoListItem item;
	private MapLocation location;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.viewtodoitem);
	
	    view = (MapView) findViewById(R.id.viewToDomapview);
	    name = (TextView) findViewById(R.id.viewToDoName);
	    category = (TextView) findViewById(R.id.viewToDoCat);
	    dueDate=(TextView) findViewById(R.id.viewToDoDueDate);
	    comment = (TextView) findViewById(R.id.viewToDoComment);
	    discription = (TextView) findViewById(R.id.viewToDoDisc);
	    editButton = (Button) findViewById(R.id.viewToDoEditButton);
	    okButton = (Button) findViewById(R.id.viewToDoOkButton);
	    mController = view.getController();
	    
	    view.setBuiltInZoomControls(true);
	    item = ToDoListItem.getItemFromIntent(getIntent());
	    
	    if(item != null && item.location != null){
	    	location = new MapLocation(this,item);
        	List<Overlay> listOfOverlays = view.getOverlays();
        	listOfOverlays.clear();
        	listOfOverlays.add(location);
        	mController.animateTo(location.getGeoPoint());
            mController.setZoom(17); 
	    }
	    name.setText(item.itemName);
	    category.setText(item.type);
	    comment.setText(item.comments);
	    discription.setText(item.description);
	    if (item.time>0) dueDate.setText(TimeAuthority.timeStampToComponent(item.time));
	    else dueDate.setText("N/A");
        view.invalidate();
        okButton.setOnClickListener(this);
        editButton.setOnClickListener(this);
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.viewToDoOkButton: this.setResult(Activity.RESULT_CANCELED);
										this.finish();
										break;
			case R.id.viewToDoEditButton:	this.setResult(ICreateToDoActivity.RESULT_OK, ToDoListItem.putItemIntoIntent(new Intent(), item));
											this.finish();
											break;
		}
		
	}

}
